<!DOCTYPE html>
<title>Verifies preload=metadata doesn't buffer the entire clip.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<div id="log"></div>
<video controls preload=metadata></video>
<script>
async_test(function(t) {
  var have_metadata = false;
  var waiting_for_metadata = false;
  var video = document.querySelector("video");

  video.onloadedmetadata = t.step_func(function() {
    have_metadata = true;
    if (waiting_for_metadata)
      video.onsuspend();
  });

  video.onsuspend = t.step_func(function() {
    // The metadata event may not fire before suspend occurs, so wait if needed;
    // while not spec'd, it'd be nice if this wasn't true, see crbug.com/455866.
    if (!have_metadata) {
      waiting_for_metadata = true;
      return;
    }

    assert_greater_than_equal(video.buffered.length, 1);
    assert_greater_than_equal(video.seekable.length, 1);

    // If the entire clip wasn't buffered, the buffered range should be smaller
    // than the seekable range; which should be the duration since the server
    // supports range requests.
    assert_true(video.buffered.end(0) < video.duration);
    assert_equals(video.seekable.end(0), video.duration);
    assert_true(video.buffered.end(0) < video.seekable.end(0),
                video.buffered.end(0) + " < " + video.seekable.end(0));
    t.done();
  });

  // Use a .webm to ensure metadata is near the beginning of the file.
  var mediaFile = "resources/media-source/webm/test.webm";
  video.src = "http://127.0.0.1:8000/media/video-throttled-load.cgi?" +
      "nph=1&name=" + mediaFile + "&throttle=40&type=video/webm";
});
</script>
